//
//  GeometryViewController.swift
//  ZSceneDemo
//
//  Created by 张鑫 on 2020/9/1.
//  Copyright © 2020 www.lionaitech.com. All rights reserved.
//

import UIKit

import SceneKit

class GeometryViewController: UIViewController {
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        createScene()
        addGeometry()
    }
    
    var scene: SCNScene?
    func createScene() {
        //设置摄像机架设点
        let cameraNode = SCNNode()
        cameraNode.position = SCNVector3(0, 25, 100)
        cameraNode.camera = SCNCamera()
        cameraNode.camera?.zFar = 200
        cameraNode.rotation = SCNVector4(1, 0, 0, -Double.pi/16)
        
        
        //设置光源点
        let lightNode = SCNNode()
        lightNode.light = SCNLight()
        lightNode.light?.type = .omni
        lightNode.position = SCNVector3(0, 10, 10)
        
        //设置周围环境
        let ambientLightNode = SCNNode()
        ambientLightNode.light = SCNLight()
        ambientLightNode.light?.type = .ambient
        ambientLightNode.light?.color = UIColor.lightGray
        
        // 创建场景，添加各个元素
        scene = SCNScene()
        
        scene?.rootNode.addChildNode(cameraNode)
        //scene?.rootNode.addChildNode(lightNode)
        //scene?.rootNode.addChildNode(ambientLightNode)
        //scene?.background.contents = "art.scnassets/SunSysSource/earth-diffuse.jpg"
        //scene?.background.intensity = 0.1
        //scene?.background.wrapT = .repeat
        //scene?.background.wrapS = .repeat
        scene?.lightingEnvironment.contents = "art.scnassets/SunSysSource/earth-diffuse.jpg"
        
        let threeDView = SCNView(frame: CGRect.zero)
        threeDView.translatesAutoresizingMaskIntoConstraints = false
        threeDView.scene = scene
        threeDView.allowsCameraControl = true
        threeDView.showsStatistics = true
        threeDView.backgroundColor = UIColor.black
        threeDView.preferredFramesPerSecond = 30
        view.addSubview(threeDView)
        
        
        threeDView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        threeDView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        threeDView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        threeDView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    }
    
    func addGeometry() {
        
        //圆环
        //        let geometry = SCNTorus(ringRadius: 10, pipeRadius: 7)
        //平面
        //        let geometry = SCNPlane(width: 10, height: 10)
        //金字塔
        //        let geometry = SCNPyramid(width: 10, height: 10, length: 10)
        //球
        //        let geometry = SCNSphere(radius: 10)
        //圆柱
        let geometry = SCNCylinder(radius: 10, height: 10)
        //文字
        //        let geometry = SCNText(string: "hello", extrusionDepth: 10)
        //领域- 球
        //        let geometry = SCNShape(path: UIBezierPath(rect: CGRect(x: 0, y: 0, width: 10, height: 10)), extrusionDepth: 10)
        geometry.firstMaterial?.diffuse.contents = "art.scnassets/texture.png"
        
        let geometryNode = SCNNode()
        geometryNode.geometry = geometry
        scene?.rootNode.addChildNode(geometryNode)
        
        getSourceVertex(sources: geometry.sources)
    }
    
    func getSourceVertex(sources: [SCNGeometrySource]) {
        
        print(sources)
        let source = sources[1]
        
        print(source.semantic.rawValue)
        print(source.vectorCount)           //294
        print(source.componentsPerVector)   //3
        print(source.bytesPerComponent)     //4
        print(source.dataOffset)            //0
        print(source.dataStride)            //32
        print("===============")
        for vectorIndex in 0..<source.vectorCount {
            let lower = source.dataStride * vectorIndex + source.dataOffset
            let upper = source.dataStride * vectorIndex + source.dataOffset + source.componentsPerVector * source.bytesPerComponent
            let vertexData = source.data.subdata(in: Range(uncheckedBounds: (lower: lower, upper: upper)))
            
            let bytes = [UInt8](vertexData)
            
            let x = Float(bitPattern: UInt32(bytes[0]) + UInt32(bytes[1]) << 8 + UInt32(bytes[2]) << 16 + UInt32(bytes[3]) << 24)
            let y = Float(bitPattern: UInt32(bytes[4]) + UInt32(bytes[5]) << 8 + UInt32(bytes[6]) << 16 + UInt32(bytes[7]) << 24)
            var z: Float? = nil
            if source.componentsPerVector >= 3 {
                z = Float(bitPattern: UInt32(bytes[8]) + UInt32(bytes[9]) << 8 + UInt32(bytes[10]) << 16 + UInt32(bytes[11]) << 24)
            }
            
            print("x:\(x), y:\(y), z:\(z)")
        }
        print(source.data)
    }
    
    
    
}
